package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class TrackInfoServiceImpl extends ServiceImpl<TrackInfoMapper, TrackInfo> implements TrackInfoService {

	@Autowired
	private TrackInfoMapper trackInfoMapper;

    @Autowired
    private AlbumInfoMapper albumInfoMapper;

    @Autowired
    private VodService vodService;

    @Autowired
    TrackStatMapper trackStatMapper;

    /**
     * 保存声音信息 根据业务逻辑需要保存三张表信息
     * album_info 更新专辑包含声音总数+1
     * track_stat 声音统计信息
     * track_stat 声音信息
     * @param userId
     * @param trackInfoVo
     */
    @Override
    public void saveTrackInfo(Long userId, TrackInfoVo trackInfoVo) {
        //1.保存声音信息
        TrackInfo trackInfo = BeanUtil.copyProperties(trackInfoVo, TrackInfo.class);
        Integer orderNum = trackInfo.getOrderNum();
        trackInfo.setUserId(userId);
        //声音在专辑中的排序值 按日常逻辑应该是有序的 所以应该为专辑中的声音总数+1
        //所以先查询专辑中的声音总数
        AlbumInfo albumInfo = albumInfoMapper.selectById(trackInfo.getAlbumId());
        Integer includeTrackCount = albumInfo.getIncludeTrackCount();
        trackInfo.setOrderNum(includeTrackCount+1);
        //声音大小 时长 和文件类型 需要从vod中获取
        TrackMediaInfoVo trackMediaInfoVo = vodService.getMediaInfo(trackInfo.getMediaFileId());
        trackInfo.setMediaSize(trackMediaInfoVo.getSize());
        trackInfo.setMediaType(trackMediaInfoVo.getType());
        trackInfo.setMediaDuration(new BigDecimal(trackMediaInfoVo.getDuration()));
        trackInfo.setSource(SystemConstant.TRACK_SOURCE_USER);
        //审核状态暂设置为已通过
        trackInfo.setStatus(SystemConstant.TRACK_STATUS_PASS);
        trackInfoMapper.insert(trackInfo);

        //初始化声音统计信息
        this.saveTrackStat(trackInfo.getId(),SystemConstant.TRACK_STAT_PLAY,0);
        this.saveTrackStat(trackInfo.getId(),SystemConstant.TRACK_STAT_COLLECT,0);
        this.saveTrackStat(trackInfo.getId(),SystemConstant.TRACK_STAT_PRAISE,0);
        this.saveTrackStat(trackInfo.getId(),SystemConstant.TRACK_STAT_COMMENT,0);

        //更新专辑中的包含声音总数
        albumInfo.setIncludeTrackCount(includeTrackCount+1);
        albumInfoMapper.updateById(albumInfo);

    }
    public void saveTrackStat(Long trackId,String statType,Integer statNum){
        TrackStat trackStat = new TrackStat();
        trackStat.setTrackId(trackId);
        trackStat.setStatType(statType);
        trackStat.setStatNum(statNum);
        trackStatMapper.insert(trackStat);
    }

    /**
     * 查询声音分页列表 请求体封装成了TrackInfoQuery对象 返回结果封装成了TrackListVo对象
     * @param pageInfo
     * @param trackInfoQuery
     * @return
     */
    @Override
    public Page<TrackListVo> findUserTrackPage(Page<TrackListVo> pageInfo, TrackInfoQuery trackInfoQuery) {
        return trackInfoMapper.selectUserTrackPage(pageInfo,trackInfoQuery);


    }

    /**
     * 更新声音信息数据
     * 业务逻辑分析后发现只需要更新声音文件信息即可
     * @param id
     * @param trackInfoVo
     */
    @Override
    public void updateTrackInfo(Long id, TrackInfoVo trackInfoVo) {
        //1.判断声音文件是否变更 先获取原声音文件id
        TrackInfo trackInfo = trackInfoMapper.selectById(id);
        String beforeMediaFileId = trackInfo.getMediaFileId();
        String afterMediaFileId = trackInfoVo.getMediaFileId();
        //创建新的对象容器
        BeanUtils.copyProperties(trackInfoVo,trackInfo);
        if(!beforeMediaFileId.equals(afterMediaFileId)){
            //说明声音文件变更 重新获取新的声音文件信息
            TrackMediaInfoVo mediaInfo = vodService.getMediaInfo(afterMediaFileId);
            trackInfo.setMediaType(mediaInfo.getType());
            trackInfo.setMediaDuration(new BigDecimal(mediaInfo.getDuration()));
            trackInfo.setMediaSize(mediaInfo.getSize());
            //需要手动将修改前的文件从腾讯云删除
            vodService.deleteTrackMedia(beforeMediaFileId);

        }
        trackInfoMapper.updateById(trackInfo);


    }

    /**
     * 删除声音信息
     * 根据业务逻辑分析 删除声音信息需要删除声音信息表 声音统计表
     * 更新专辑信息表中包含声音总数
     * 删除声音表的时候为了保证集数的连贯性 需要从删除声音的下一集开始 声音集数-1
     * @param id
     * @return
     */

    public void removeTrackInfo(@PathVariable Long id){
        //1.根据id删除基本信息表 在此之前需要先获取当前声音序号 以便于
        //删除后更改其他声音序号
        TrackInfo trackInfo = trackInfoMapper.selectById(id);
        Integer orderNum = trackInfo.getOrderNum();
        trackInfoMapper.deleteById(id);
        //更新删除行以后的所有声音信息的orderNum
        trackInfoMapper.updateTrackInfo(trackInfo.getAlbumId(),orderNum);
        //2.根据trackId删除声音统计记录
        QueryWrapper<TrackStat>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("track_id",id);
        trackStatMapper.delete(queryWrapper);
        //3.根据专辑id更新专辑包含声音总数
        AlbumInfo albumInfo = albumInfoMapper.selectById(trackInfo.getAlbumId());
        albumInfo.setIncludeTrackCount(albumInfo.getIncludeTrackCount()-1);
        albumInfoMapper.updateById(albumInfo);
        //4.删除云点播平台声音文件
        vodService.deleteTrackMedia(trackInfo.getMediaFileId());

    }


}
